package youleche.com.business;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import youleche.com.connection.ConnectionFactory;
import youleche.com.connection.JdbcHelper;
import youleche.com.factory.DaoFactory;
import youleche.com.idao.IRentVehicleInfo;
import youleche.com.model.MRentVehicleInfo;

/**
 * 租车业务层
 * @author Administrator
 *
 */
public class BRentVehicleInfo {
	/**
	 * 处理器
	 */
	private IRentVehicleInfo handler = null;
	
	/**
	 * 数据库链接
	 */
	private Connection con = null;
	
	/**
	 * 默认构造函数
	 */
	public BRentVehicleInfo(){
		this.handler = DaoFactory.getDRentVehicleInfo();
	}
	
	/**
	 * 添加租车信息
	 * @param rentVehicleInfo 租车信息
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	public int addRentVehicleInfo(
			MRentVehicleInfo rentVehicleInfo) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.handler.addRentVehicleInfo(this.con, rentVehicleInfo);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据id得到租车信息
	 * @param rentId 租车ID
	 * @return 返回租车信息
	 * @throws Exception 异常
	 */
	public MRentVehicleInfo getRentVehicleInfoById(String rentId)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getRentVehicleInfoById(this.con, rentId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 得到用户今天发布的租车信息数量
	 * @param releaseId 发布者id
	 * @return 返回租车信息数量
	 * @throws Exception 异常
	 */
	public String getRentVehicleInfoByUserTodayNums(
			String releaseId) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getRentVehicleInfoByUserTodayNums(this.con, releaseId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 获取租车信息列表
	 * @param startIndex 开始索引
	 * @param endIndex 每次获取条数
	 * @return 返回列表信息
	 * @throws Exception 异常
	 */
	public List<MRentVehicleInfo> getRentVehicleInfoForList(
			String startIndex, String endIndex, String orderBy) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getRentVehicleInfoForList(this.con, startIndex, endIndex, orderBy);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据租车id更新图片信息
	 * @param imageId 图片ID
	 * @param rentId 租车信息id
	 * @return 返回更新影响的行数
	 * @throws Exception 异常
	 */
	public int updateImageIdByRentId(String imageId, String rentId)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.handler.updateImageIdByRentId(this.con, imageId, rentId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据id更新相关度（访问量）
	 * @param id id
	 * @param a4 相关度
	 * @return 返回更新影响的行数
	 * @throws exception 异常
	 */
	public int updateA4ById(String id, int a4)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.handler.updateA4ById(this.con, id, a4);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**根据用户名得到租车信息总条数
	 * 
	 * @param userName 用户名
	 * @return 返回租车信息总条数
	 * @throws Exception 异常
	 */
	public int getRentInfoCountByUserName( String userName)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getRentInfoCountByUserName(this.con, userName);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据用户名获取租车信息列表
	 * @param startIndex 开始索引
	 * @param endIndex 每次获取条数
	 * @param userName 用户名
	 * @return 返回列表信息
	 * @throws Exception 异常
	 */
	public List<MRentVehicleInfo> getRentVehicleInfoForListByUserName(
			String startIndex, String endIndex, String userName)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getRentVehicleInfoForListByUserName(this.con, startIndex, endIndex, userName);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据租车ID删除租车信息
	 * @param id Id
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	public int deleteRentInfoById(String id) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.handler.deleteRentInfoById(this.con, id);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 获取租车信息总条数
	 * @return 返回租车信息总条数
	 * @throws Exception 异常
	 */
	public int getRentInfoCount() throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getRentInfoCount(this.con);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据id得到推荐租车信息
	 * @param con 数据库连接
	 * @return 返回租车信息
	 * @throws Exception 异常
	 */
	public MRentVehicleInfo getRecommendRentVehicleInfo()
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getRecommendRentVehicleInfo(this.con);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 获取下一条车辆信息
	 * @param addTime 本条车辆信息添加时间
	 * @return 返回租车信息
	 * @throws Exception 异常
	 */
	public MRentVehicleInfo getNextVehicleInfo(String addTime)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getNextVehicleInfo(this.con, addTime);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}

	/**
	 * 获取上一条车辆信息
	 * @param addTime 本条车辆信息添加时间
	 * @return 返回租车信息
	 * @throws Exception 异常
	 */
	public MRentVehicleInfo getPreVehicleInfo(String addTime)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getPreVehicleInfo(this.con, addTime);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
}
	
